package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    //新增员工
    @Override
    public void add(Employee employee) {
    //补全员工信息
        // 1-1 雪花算法ID生成器
        long id = IdUtil.getSnowflake(1, 1).nextId();
        employee.setId(id);
        // 1-2 初始密码
        employee.setPassword(SecureUtil.md5(Constant.INIT_PASSWORD));
        // 1-3 员工状态
        employee.setStatus(1);
        // 1-4 创建、更新时间
        /*employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
        // 1-5 创建、更新人
        employee.setCreateUser(1l);
        employee.setUpdateUser(1l);*/
        // 2.调用mapper新增
        employeeMapper.add(employee);
    }

    // 员工登录
    @Override
    public ResultInfo login(String username, String password) { // password = 123456
        // 1.根据username查询mapper
        Employee employee = employeeMapper.findByUsername(username);
        if (employee == null) {
            return ResultInfo.error("此用户不存在..");
        }
        // 2.密码比较
        // 2-1 将前端密码进行md5加密
        String md5 = SecureUtil.md5(password);
        // 2-2 跟数据库比较
        if (!md5.equals(employee.getPassword())) {
            return ResultInfo.error("密码错误...");
        }

        // 3.判断此账号是否被禁用
        if (employee.getStatus() == 0) { // 1：正常、0：禁用
            return ResultInfo.error("此账号被禁用，请联系管理员");
        }
        // 4.登录成功，返回用户信息
        return ResultInfo.success(employee);//返回给resultInfo
    }


    @Override
    public List<Employee> findList(String name) {
        return employeeMapper.findList(name);
    }
    //回显数据
    @Override
    public Employee findById(Long id) {
        //调用EmployeeMapper方法
        Employee employee=employeeMapper.findById(id);
        //返回对象
        return employee;

    }
    //修改员工信息
    @Override
    public void updateById(Employee employee) {
        //补全信息
        //生成雪花id
        //long id = IdUtil.getSnowflake(2, 2).nextId();
        //employee.setId(id);
        //生成MD5密码
        //String password = SecureUtil.md5(employee.getPassword());
        //employee.setPassword(password);
        //// 1-3 员工状态
        //employee.setStatus(1);
        // 1-4 创建、更新时间
       /* employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
        // 1-5 创建、更新人
        employee.setCreateUser(1l);
        employee.setUpdateUser(1l);*/
        // 2.调用mapper新增
        employeeMapper.update(employee);
    }


}
